//
//  HUD.m
//  FlagForiOS
//
//  Created by Steven.Yung on 2016/11/24.
//  Copyright © 2016年 Minidai. All rights reserved.
//

#import "NetworkProgressHUD.h"
#import "MBProgressHUD.h"
#import "UIView+Toast.h"
#import "UIView+JKToast.h"

#define kKeyWindow [UIApplication sharedApplication].keyWindow

@implementation NetworkProgressHUD

+ (void)showHudNetworkError:(NSError *)error {
    
    MBProgressHUD *hud = [MBProgressHUD HUDForView:kKeyWindow];
    if (hud) {
        hud.removeFromSuperViewOnHide = YES;
        [hud hideAnimated:YES];
    }
    NSString *errMsg = @"";
    switch (error.code) {
            
        case NSURLErrorTimedOut:
        {
            errMsg = @"请求超时，请重试";
            
        }
            break;
        case NSURLErrorNotConnectedToInternet:
        {
            errMsg = @"无法连接网络，请稍后再试";
        }
            break;
            
        case NSURLErrorCannotFindHost:
        {
            errMsg = @"找不到服务器，请稍后再试";
        }
            
            break;
            
        case NSURLErrorCannotConnectToHost:
        {
            errMsg = @"服务器无法连接";
        }
            
            break;
            
        case NSURLErrorNetworkConnectionLost:
        {
            errMsg = error.localizedDescription;
        }
            break;
            
        default:
            errMsg = [NSString stringWithFormat:@"服务器错误(错误码:%ld)", (long)error.code];
            break;
    }
    
    [kKeyWindow jk_makeToast:errMsg duration:2.0 position:JKToastPositionCenter];
}

+ (void)showHudMsg:(NSString *)msg{
    if (!msg || msg.length <= 0) {
        return;
    }
    dispatch_async(dispatch_get_main_queue(), ^{
        MBProgressHUD *hud = [MBProgressHUD HUDForView:kKeyWindow];
        if (hud) {
            hud.removeFromSuperViewOnHide = YES;
            [hud hideAnimated:YES];
        }
        [kKeyWindow jk_makeToast:msg duration:2.0 position:JKToastPositionCenter];
    });
}

+ (void)showHud {
    dispatch_async(dispatch_get_main_queue(), ^{
        MBProgressHUD *hud = [MBProgressHUD HUDForView:kKeyWindow];
        if (!hud) {
//            hud.removeFromSuperViewOnHide = YES;
//            [hud hideAnimated:YES];
            [MBProgressHUD showHUDAddedTo:kKeyWindow animated:YES];
        }
        
    });
    
}

+ (void)hideHud {
    dispatch_async(dispatch_get_main_queue(), ^{
//        MBProgressHUD *hud = [MBProgressHUD HUDForView:kKeyWindow];
//        if (hud) {
//            hud.removeFromSuperViewOnHide = YES;
//            [hud hideAnimated:YES];
//        }
        [MBProgressHUD hideHUDForView:kKeyWindow animated:YES];
    });
}


@end
